package org.gruposp2p.proyectokarma.gwt.client.widget;

import org.gruposp2p.proyectokarma.gwt.client.MainEntryPoint;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;

public class ClickableLabel extends Composite {
	
	public enum Type {TAG,  OPTION}
	
    Label nameLabel;
    String tagName;
    Image image;

    Type labelType; 
    private Handler handler;

	public ClickableLabel (String tagName, Handler handler, Type labelType) {
		this.handler = handler;
		this.tagName = tagName;
		this.labelType = labelType;
		image = new Image(MainEntryPoint.INSTANCE.resources.tagLabelIcon());
		HorizontalPanel widget = new HorizontalPanel();
        initWidget(widget);
		image.addClickHandler(new IconLabelClickHandler());
		image.addMouseOverHandler(new OverHandler());
		image.addMouseOutHandler(new DownHandler());
		widget.add(image);
		image.setStylePrimaryName("iconTag");
		nameLabel = new Label(tagName);
        Style linkStyle = (Style) nameLabel.getElement().getStyle();
        linkStyle.setProperty("fontSize", "15");
		widget.add(nameLabel);
		widget.setStylePrimaryName("clickableLabel");
	}
	
	public Type getType () {
		return labelType;
	}
	
    private class IconLabelClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			processIconClick();
		}	
	}
    
    private class OverHandler implements MouseOverHandler {

		@Override
		public void onMouseOver(MouseOverEvent event) {
			image.setResource(MainEntryPoint.INSTANCE.resources.tagLabelHoverIcon());
		}	
	}

    
    private class DownHandler implements MouseOutHandler {
		@Override
		public void onMouseOut(MouseOutEvent event) {
			image.setResource(MainEntryPoint.INSTANCE.resources.tagLabelIcon());
		}	
	}
    
    private void processIconClick () {
    	handler.processIconClick(this);
    }
    
    public interface Handler {
    	void processIconClick(ClickableLabel tagLabel);
    }
    
    public String getText () {
    	return tagName;
    }

}
